﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using OSharp.Core.Data;
using OSharp.Core.Data.Extensions;
using OSharp.Utility.Filter;
using OSharp.Web.Mvc;
using OSharp.Web.Mvc.Logging;
using Serenity.Libra.Web.ViewModels;
using Serenity.Utility.Mvc.UI;

namespace Serenity.Libra.Web.Controllers
{
    /// <summary>
    /// 后台管理控制器蕨类
    /// </summary>
    [OperateLogFilter]
    public abstract class AdminBaseController : BaseController
    {
        #region 字段
        protected virtual string[] ExcludeProperties
        {
            get { return null; }
        }

        protected FormCollection _formCollection;
        /// <summary>
        /// POST回来的表单值
        /// </summary>
        protected FormCollection FormData
        {
            get
            {
                if (_formCollection == null)
                    _formCollection = new FormCollection(Request.Form);
                return _formCollection;
            }
        }

        protected DetailPageMode pageMode;
        protected DetailPageMode PageMode
        {
            get
            {
                var mode = Request.QueryString["mode"];
                if (!string.IsNullOrEmpty(mode))
                {
                    pageMode = (DetailPageMode)Enum.Parse(typeof(DetailPageMode), mode, true);
                }
                else
                {
                    pageMode = DetailPageMode.New;
                    if (RouteData.Values.ContainsKey("id"))
                    {
                        var id = RouteData.Values["id"];
                        if (id.ToString() != Guid.Empty.ToString() && id.ToString() != "0")
                        {
                            pageMode = DetailPageMode.Edit;
                        }
                    }
                }
                return pageMode;
            }
        }

        #endregion

        protected virtual IQueryable<TEntity> GetQueryData<TEntity, TKey>(IQueryable<TEntity> source, out int total, MiniGridRequest request = null)
            where TEntity : EntityBase<TKey>
        {
            if (request == null)
            {
                request = new MiniGridRequest(Request);
            }
            Expression<Func<TEntity, bool>> predicate = FilterHelper.GetExpression<TEntity>(request.FilterGroup);
            return source.Where(predicate, request.PageCondition, out total);
        }

        protected virtual PageResult<TResult> GetPageResult<TEntity, TResult>(IQueryable<TEntity> source,
            Expression<Func<TEntity, TResult>> selector,
            MiniGridRequest request = null)
        {
            if (request == null)
            {
                request = new MiniGridRequest(Request);
            }
            Expression<Func<TEntity, bool>> predicate = FilterHelper.GetExpression<TEntity>(request.FilterGroup);
            return source.ToPage(predicate, request.PageCondition, selector);
        }

        public virtual ActionResult Index()
        {
            return View();
        }
    }
}